﻿//Sys.Application.add_init(pageInit);
jQuery.fn.extend({
    slideRightShow: function (speed, callback) {
        return this.each(function () {
            $(this).show('slide', { direction: 'right' }, speed, callback);
        });
    },
    slideLeftHide: function (speed, callback) {
        return this.each(function () {
            $(this).hide('slide', { direction: 'left' }, speed, callback);
        });
    },
    slideRightHide: function (speed, callback) {
        return this.each(function () {
            $(this).hide('slide', { direction: 'right' }, speed, callback);
        });
    },
    slideLeftShow: function (speed, callback) {
        return this.each(function () {
            $(this).show('slide', { direction: 'left' }, speed, callback);
        });
    }
});

$(document).ready(function () {
    $('#loadingDialog').dialog({
        height: 300,
        width: 500,
        resizable: false,
        draggable: false,
        closeOnEscape: false,
        autoOpen: false,
        modal: true
    });
    $('#loadingDialog').ajaxStart(function () {
        $(this).dialog('open');
    });
    $('#loadingDialog').ajaxStop(function () {
        $(this).dialog('close');
    });
    var removeTab = function (tabSelector) {
        $(".removeTab:last").click(function () {
            var index = $('li', tabSelector).index($(this).parent());
            tabSelector.tabs('remove', index);
        });
    };
    var tabs = $("#tabs").tabs({
        add: function (event, ui) {
            //select newly opened tab
            $(this).tabs('select', ui.index);

            //load function to close tab
            removeTab($(this));
        },
        load: function (event, ui) {
            $(".addTab", ui.panel).click(addNewTab);
            $(".buttonContainer a", ui.panel).button();
        },
        tabTemplate: '<li><a href="#{href}">#{label}</a><span class="ui-icon ui-icon-close removeTab"></span></li>',
        cache: true
    });
    addTab();
});

function addTab() {
    $(".addTab").click(addNewTab);
}

function addNewTab(event) {
    event.preventDefault();
    var href = $(this).attr("href");
    var title = $(this).attr("title");
    $("#tabs").tabs('add', href, title);
}

//function pageInit() {
//    // Enable history
//    Sys.Application.set_enableHistory(true);

//    // Add Handler for history
//    Sys.Application.add_navigate(navigate);
//}

//function navigate(sender, e) {
//    var state = e.get_state();
//    var tabIndex = state.tabIndex ? state.tabIndex : 0;
//    $('#tabs').tabs('select', tabIndex);
//}

function post(link, data, updateTarget, slideOutTarget) {
    if (updateTarget && updateTarget != null)
        updateTarget.hide();
    jQuery.ajax({
        type: 'POST',
        data: data,
        url: link.attr("href"),
        success: function (result) {
            if (updateTarget && updateTarget != null) {
                updateTarget.html(result);
                $(".addTab", updateTarget).click(addNewTab);
                $(".buttonContainer a", updateTarget).button();
                $('.validation-summary-errors ul', updateTarget).addClass('ui-state-error');
                $('.validation-summary-errors ul li', updateTarget).prepend('<span class=\'ui-icon ui-icon-alert\'></span>');
            }

            if (slideOutTarget && slideOutTarget != null)
                slideOutTarget.slideLeftHide('slow', function () { updateTarget.slideRightShow('slow'); });
            else if (updateTarget && updateTarget != null)
                updateTarget.slideDown();
        }
    });
}

function goBack(childTarget, parentTarget) {
    childTarget.slideRightHide('slow', function () { parentTarget.slideLeftShow('slow'); });
}